<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <el-upload
            action="#"
            list-type="picture-card"
            :limit="2"
            :auto-upload="true"
            :before-upload="beforeUploadForm"
            :http-request="imageChange">
        <div>上传文件</div>
    </el-upload>
</div>

</body>
<script>
    // var data={tu:''}
    var app=new Vue({
        el:'#app',
        // data:data,
        data:{
            tu:''
        },
        methods: {
            // 开始上传前验证
            beforeUploadForm: function (file) {
                // 验证文件类型
                var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
                var extension = testmsg === 'jpg' || testmsg === 'png' || testmsg === 'gif'
                if (!extension) {
                    this.$message({
                        message: '上传文件只能是jpg/png/gif格式!',
                        duration: 1000,
                        showClose: true,
                        type: 'warning'
                    })
                }
                return extension
            },
            // 提交图片
            imageChange: function (param, type) {
                var formData = new FormData()
                formData.append('file', param.file);
                axios.post('/uploadLocal', formData).then(function (res) {
                    console.log(res);
                });
            }
        }
    });
</script>
</body>
</html>